package at.fhooe.im440.pandora.graphicsConfig;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferStrategy;

public class WindowScreen implements Screen {

	private BufferStrategy bufferStrategy;
	private Graphics2D drawGraphics;
	private RenderSystem rs;
	
	
	private int width;
	private int height;
	
	public WindowScreen (RenderSystem rs) {
		this.rs = rs;
	}
	
	@Override
	public void open(int width, int height) {
		
		this.width = width;
		this.height = height;
		
		rs.setIgnoreRepaint(true);
		rs.setResizable(false);
		rs.requestFocus();
		Canvas windowCanvas = new Canvas();
		windowCanvas.setPreferredSize(new Dimension(width, height));
		rs.add(windowCanvas);
		rs.pack();
		rs.setVisible(true);
		windowCanvas.createBufferStrategy(2);
		bufferStrategy = windowCanvas.getBufferStrategy();
	}

	@Override
	public void close() {
		rs.dispose();
	}

	@Override
	public Graphics2D beginUpdate() {
		drawGraphics = (Graphics2D) bufferStrategy.getDrawGraphics();
		AffineTransform at = new AffineTransform();
		at.setToIdentity();
		at.translate(width/2, height/2);
		at.scale(1, -1);
		drawGraphics.transform(at);
		return drawGraphics;
	}

	@Override
	public void endUpdate() {
		drawGraphics.dispose();
		bufferStrategy.show();
		Toolkit.getDefaultToolkit().sync();
	}

}
